package ua.org.nuos.wclientgui.utils;

import org.apache.wicket.Page;
import org.apache.wicket.authorization.strategies.page.AbstractPageAuthorizationStrategy;
import ua.org.nuos.sdms.middle.entity.enums.Role;
import ua.org.nuos.wclientgui.pages.MainPage;
import ua.org.nuos.wclientgui.pages.StartPage;

/**
 * Created with IntelliJ IDEA.
 * User: dio
 * Date: 9/16/12
 * Time: 7:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class ClientAuthorizer extends AbstractPageAuthorizationStrategy {

    private static final Class<?>[] classes = new Class<?>[]{MainPage.class};

    private ServiceLocator serviceLocator;

    public ClientAuthorizer(ServiceLocator serviceLocator) {
        this.serviceLocator = serviceLocator;
    }

    @Override
    protected <T extends Page> boolean isPageAuthorized(Class<T> pageClass) {
        for (Class<?> c : classes) {
            if (c.equals(pageClass)) {
                return isClientAuthorized();
            }
        }
        return true;
    }

    private boolean isClientAuthorized() {
        return serviceLocator.getAuthenticationBean().isUserInRole(Role.client);
    }
}
